package com.shaiMiMi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;


import android.R.integer;

interface ZXmlObj //支持生成xml内容的接口
{
	public abstract boolean ExchangeByXml(ZXmlDoc xmlDoc, Element parentElem);
};

public class ZXmlDoc {
	private Element mRoot;
	public boolean mLoadFrom = false;
	public Document mDom;
	public static final String ValAttr = "value";
	public static final String TypeAttr = "type";
	public static final String PackageClassAttr = "ClassName";

	public void InitLoadFrom(String sXmlStr){
		mLoadFrom = true;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			ByteArrayInputStream inStream = new ByteArrayInputStream(sXmlStr.getBytes("ISO-8859-1"));
			mDom = builder.parse(inStream);
			mRoot = mDom.getDocumentElement();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void InitSaveTo(){
		mLoadFrom = false;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			mDom = builder.newDocument();
			mRoot = mDom.createElement("root");
			mDom.appendChild(mRoot);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	public String GetXmlStr(ZPackage pack){
		String classNameString = pack.getClass().getName();
		int iLastIndex = classNameString.lastIndexOf(".");
		if (iLastIndex > -1) {
			classNameString = classNameString.substring(iLastIndex+1);
		}
		mRoot.setAttribute(PackageClassAttr, classNameString);

		TransformerFactory   tf   =   TransformerFactory.newInstance();
		Transformer t = null;

		try {
			t = tf.newTransformer();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		}

//		t.setOutputProperty(/"encoding/",/"GB23121/");//解决中文问题，试过用GBK不行
		ByteArrayOutputStream   bos   =   new   ByteArrayOutputStream();

		try {
			t.transform(new DOMSource(mDom), new StreamResult(bos));
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		String xmlStr = bos.toString();
		return xmlStr;
	}
	//
	public Element GetRootElem() {
		return mRoot;
	}
	//
	public Element AppendChild(Element parentElem, String elemName){
		Element newElement = mDom.createElement(elemName);
		parentElem.appendChild(newElement);
		return newElement;
	}

	public Element AppendChild(Element parentElem, String elemName, integer iElemVal){
		Element newElement = mDom.createElement(elemName);
		newElement.setAttribute(ValAttr, iElemVal.toString());
		parentElem.appendChild(newElement);
		return newElement;
	}

	public Element AppendChild(Element parentElem, String elemName, Double dElemVal){
		Element newElement = mDom.createElement(elemName);
		newElement.setAttribute(ValAttr, dElemVal.toString());
		parentElem.appendChild(newElement);
		return newElement;
	}

	public Element AppendChild(Element parentElem, String elemName, String sElemVal){
		Element newElement = mDom.createElement(elemName);
		newElement.setAttribute(ValAttr, sElemVal);
		Node newNode = parentElem.appendChild(newElement);
		return newElement;
	}

	public Element AppendChild(Element parentElem, String elemName, byte[] byteArr){
		Element newElement = mDom.createElement(elemName);
		newElement.setAttribute(ValAttr, ZHexConver.byte2HexStr(byteArr));
		parentElem.appendChild(newElement);
		return newElement;
	}

	public Element AppendChild(Element parentElem, String elemName, ZXmlObj xmlObj){
		Element newNode = AppendChild(parentElem, elemName);
		newNode.setAttribute(TypeAttr, xmlObj.getClass().getName());
		xmlObj.ExchangeByXml(this, newNode);
		return newNode;
	}
	//
	public boolean GetVal(Element docElem, Integer iElemVal){
		if (!docElem.hasAttribute(ValAttr))
		{
			return false;
		}

		String sVal = docElem.getAttribute(ValAttr);
		if (sVal.length() > 0) {
			iElemVal = Integer.parseInt(sVal);
			return true;
		}

		return false;
	}

	public boolean GetVal(Element docElem, Double dElemVal){
		if (!docElem.hasAttribute(ValAttr))
		{
			return false;
		}

		String sVal = docElem.getAttribute(ValAttr);
		if (sVal.length() > 0) {
			dElemVal = Double.parseDouble(sVal);
			return true;
		}

		return false;
	}

	public String GetStrVal(Element docElem){
		if (!docElem.hasAttribute(ValAttr))
		{
			return null;
		}

		return docElem.getAttribute(ValAttr);
	}

	public byte[] GetVal(Element docElem){
		if (!docElem.hasAttribute(ValAttr))
		{
			return null;
		}

		String sVal = docElem.getAttribute(ValAttr);
		if (sVal.length() > 0) {
			return null;
		}

		return ZHexConver.hexStr2Bytes(sVal);
	}

	public boolean GetVal(Element docElem, ZXmlObj xmlObj){
		if (xmlObj == null)
		{
			return false;
		}

		return xmlObj.ExchangeByXml(this, docElem);
	}


}
